# Maintainer: Ricky Wu <rickleaf.wu@gmail.com>
# Contributor: ImperatorS79 <fevrier.simon@gmail.com>
# Contributoor: Ray Donnelly <mingw.android@gmail.com>

_realname=luarocks
pkgbase=mingw-w64-lua-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-lua-${_realname}")
pkgver=3.11.1
pkgrel=1
pkgdesc="the package manager for Lua modules (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://luarocks.org"
msys2_references=(
  'archlinux: luarocks'
)
license=("spdx:MIT")
depends=("${MINGW_PACKAGE_PREFIX}-lua"
         "unzip"
         "zip")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
install=${_realname}-${MSYSTEM}.install
source=("https://luarocks.org/releases/${_realname}-${pkgver}.tar.gz"
        "0001-luarocks_msys2_mingw_w64.patch"
        "0002-fix-CC-clang64-clangarm64.patch::https://github.com/luarocks/luarocks/pull/1754.patch"
        "0003-fix-package-removal.patch::https://github.com/luarocks/luarocks/pull/1756.patch")
sha256sums=('c3fb3d960dffb2b2fe9de7e3cb004dc4d0b34bb3d342578af84f84325c669102'
            '48e2e8c9cd1eaf7f18b38c25d474a4cd39dddb88b92152e278d04dffdcda2a90'
            'd13210147a4d17a4b9d731e2ca944fb22f93b49b3734d78ef02bbc2b0f8ceb7d'
            '4f600ec5a98d4b038c603be4e69af2f3be72528a73afaa959ed7829047f563f0')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/0001-luarocks_msys2_mingw_w64.patch

  # Fixes https://github.com/msys2/MINGW-packages/issues/23493
  # This patch should be removed on any release after 3.11.1
  patch -p1 -i "${srcdir}"/0002-fix-CC-clang64-clangarm64.patch

  # Fixes https://github.com/luarocks/luarocks/issues/1755
  # This patch should also be removed on any release after 3.11.1
  patch -p1 -i "${srcdir}"/0003-fix-package-removal.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  ./configure --prefix=${MINGW_PREFIX} --lua-version=5.4 --with-lua-interpreter=lua.exe
  make ./build/luarocks ./build/luarocks-admin ./build/config-5.4.lua
  make LUA_VERSION=5.1 LUA_INTERPRETER=lua5.1.exe LUA_INCDIR=${MINGW_PREFIX}/include/lua5.1 ./build/config-5.1.lua
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make DESTDIR="${pkgdir}" install
  make DESTDIR="$pkgdir" LUA_VERSION=5.1 install-config
}
